#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time
import datetime
from selenium.webdriver.support.ui import Select

import utils
from config import PROGRAM_URL


def check_browser(func):
    def wrapper(self, *args, **kwargs):
        if not self._browser:
            print("Failed to get _browser")
        else:
            func(self, *args, **kwargs)

    return wrapper


class Program(object):
    def __init__(self, browser=None):
        self._browser = browser if browser else utils.get_browser()
        print("program browser %x" % id(self._browser))

    @check_browser
    def create(self, fatherId=''):
        try:
            self._browser.get(PROGRAM_URL % fatherId)

            # 填写节目名称：Lancashire
            program_name = self._browser.find_element_by_name("pname")
            program_name.send_keys("PPC-Lancashire")

            # 设置节目类型：演唱会
            type_select = Select(self._browser.find_element_by_name('type'))
            type_select.select_by_index(1)

            # 设置频道信息：综艺
            channelCateTag_select = Select(
                self._browser.find_element_by_name('channelCateTag'))
            channelCateTag_select.select_by_index(3)

            # 设置二级分类：晚会
            category_tag = self._browser.find_elements_by_name('sec_category')
            category_tag[0].click()

            # 设置节目等级：C类三级
            episodeGrade_select = Select(
                self._browser.find_element_by_name('episodeGrade'))
            episodeGrade_select.select_by_index(9)

            # 设置推荐等级：不推荐
            recommendRate_select = Select(
                self._browser.find_element_by_name('recommendRate'))
            recommendRate_select.select_by_index(0)

            # 设置付费状态：免费
            freeStatus_radios = self._browser.find_elements_by_name('freeStatus')
            freeStatus_select = freeStatus_radios[0]
            freeStatus_select.click()

            # 设置节目开始时间：当前时间
            now = datetime.datetime.now()
            start_play_time = self._browser.find_element_by_id('startPlayTime')
            start_play_time.clear()
            start_play_time.send_keys(now.strftime('%Y-%m-%d %H:%M:%S'))

            now_plus_time = now + datetime.timedelta(hours=6)
            end_play_time = self._browser.find_element_by_id('endPlayTime')
            end_play_time.clear()
            end_play_time.send_keys(now_plus_time.strftime('%Y-%m-%d %H:%M:%S'))

            start_stream_time = self._browser.find_element_by_id(
                'streamStartPlayTime')
            start_stream_time.clear()
            start_stream_time.send_keys(now.strftime('%Y-%m-%d %H:%M:%S'))

            end_stream_time = self._browser.find_element_by_id('streamEndPlayTime')
            end_stream_time.clear()
            end_stream_time.send_keys(now_plus_time.strftime('%Y-%m-%d %H:%M:%S'))

            # 填写一句话推荐：你若精彩，上天自有安排！
            recommendRemark_text = self._browser.find_element_by_id(
                "recommendRemark")
            recommendRemark_text.send_keys(u"你若精彩，上天自有安排！")

            # 填写节目简介：你若精彩，上天自有安排！
            description_text = self._browser.find_element_by_id("description")
            description_text.send_keys(u"你若精彩，上天自有安排！")

            # 设置项目类型：非销售类型
            project_type_select = Select(
                self._browser.find_element_by_name('project_type_select'))
            project_type_select.select_by_index(2)

            # 设置是否允许进入热门直播：否
            hot_radios = self._browser.find_elements_by_name('hot')
            hot_select = hot_radios[1]
            hot_select.click()

            # 设置聊天室人工审核开关：是
            check_type_radios = self._browser.find_elements_by_name('check_type')
            check_type_select = check_type_radios[0]
            check_type_select.click()

            # 设置发言权限设置：vip用户发言
            chatRights_select = Select(
                self._browser.find_element_by_name('chatRights'))
            chatRights_select.select_by_index(1)

            # 设置是否关联璞玉：否
            puyu_radios = self._browser.find_elements_by_name('use_puyu_radio')
            puyu_select = puyu_radios[1]
            puyu_select.click()

            # 设置是否测试节目：是
            test_radios = self._browser.find_elements_by_name('test')
            test_select = test_radios[0]
            test_select.click()

            # 点击提交按钮
            submit = self._browser.find_element_by_class_name("btn-orange")
            submit.click()

            # 点击继续创建按钮
            button = self._browser.find_element_by_name("con_created")
            button.click()

        except Exception as e:
            print(e)
